from django.shortcuts import render, HttpResponse, redirect
import json
from utils.django_redis_cc import conn
from db.models import *
from datetime import datetime
from utils.login_verify import verify


# Create your views here.
# 个人中心
@verify
def udai_welcome(request, user):
    if request.method == 'GET':
        user = user
        my_order = Order.objects.filter(user_id=user.id)
        print(my_order)
        return render(request, 'user/udai_welcome.html', locals())


# 个人资料中心
@verify
def udai_setting(request, user):
    if request.method == 'GET':
        content = {'user': user}
        return render(request, 'user/udai_setting.html', locals())
    elif request.method == 'POST':
        n_id = request.POST.get('n_id')
        sex = request.POST.get('sex')
        if sex is None:
            sex = '保密'
        sex_id = Sex.objects.get(sex=sex).id
        user.username = n_id
        user.save()
        UserInfo.objects.filter(user_id=user.id).update(sex_id=sex_id)
        request.session['username'] = n_id
        return render(request, 'user/udai_welcome.html', locals())


# 积分
@verify
def udai_integral(request, user):
    if request.method == 'GET':
        user = user
        return render(request, 'user/udai_integral.html', locals())


# 会员提升
def agent_level(request):
    return render(request, 'user/agent_level.html')


# 资金管理
def udai_treasurer(request):
    return render(request, 'user/udai_treasurer.html')


# 收货地址把省渲染过去
def udai_address(request):
    phone = request.session['phone']
    user_info = User.objects.get(phone=phone)
    ao = AreaInfo.objects.filter(pid_id=None)
    address_info = Address.objects.filter(user_id=user_info.id)
    print(address_info)
    content = {'ao': ao, 'address_info': address_info}
    return render(request, 'user/udai_address.html', content)


# 获得地区列表
def get_city(request):
    # 根据省,找市区11
    address = request.GET.get('pcode')

    areas = AreaInfo.objects.filter(pid_id=address)
    city_list = []
    for x in areas:
        b = {'city': x.area_name, 'city_id': x.id, 'pid_id': x.pid_id}
        city_list.append(b)

    return HttpResponse(json.dumps({'result': city_list}))


# 获得县城列表
def get_area(request):
    # 根据市,找县城
    address = request.GET.get('pcode')

    areas = AreaInfo.objects.filter(pid_id=address)
    area_list = []
    for x in areas:
        b = {'area': x.area_name, 'area_id': x.id, 'city_id': x.pid_id}
        area_list.append(b)

    return HttpResponse(json.dumps({'result': area_list}))


# 增加收货地址
def add_address(request):
    if request.method == 'POST':
        phone = request.session['phone']
        user_info = User.objects.get(phone=phone)
        username = request.POST.get('username')
        province = request.POST.get('province')
        town = request.POST.get('city')
        district = request.POST.get('area')
        address = request.POST.get('address')
        user_phone = request.POST.get('user_phone')
        print(user_phone)
        print(province, town, district)
        Address.objects.create(address=address, province_id=province, district_id=district, town_id=town,
                               user_id=user_info.id, people=username, phone=user_phone)
        content = {'message': '添加成功'}
        return redirect("user:udai_address")
    else:
        return HttpResponse('22222222222')


# 调整修改收货地址的页面

def udai_address_edit(request, id):
    print(id)
    addob = Address.objects.get(pk=id)
    ao = AreaInfo.objects.filter(pid_id=None)
    content = {'addob': addob, 'ao': ao}
    return render(request, 'user/udai_address_edit.html', content)


# 修改收货地址
def update_address(request):
    if request.method == 'POST':
        address_id = request.POST.get('id')
        addo = Address.objects.get(id=address_id)
        addo.people = request.POST.get('username')
        addo.province_id = request.POST.get('province')
        addo.town_id = request.POST.get('city')
        addo.district_id = request.POST.get('area')
        addo.address = request.POST.get('address')
        addo.phone = request.POST.get('phone')
        if request.POST.get('checked') == 'on':
            addo.status = 1
            addo.save()
        else:
            addo.status = 0
            addo.save()
        addo.save()
        return render(request, 'user/udai_address_edit.html', {'message': '修改成功'})
    else:
        return render(request, 'user/udai_address_edit.html', {'message': '格式错误'})


# 删除收货地址
def delete_address(request, id):
    print(id)
    addo = Address.objects.get(pk=id)
    print(addo, 'bbbbbbbbbbbb')
    addo.delete()
    ao = Address.objects.all()
    content = {'addo': addo}
    return render(request, "user/udai_address.html", content)


# 优惠券
def udai_coupon(request):
    return render(request, 'user/udai_coupon.html')


# 修改支付密码
def udai_paypwd_modify(request):
    return render(request, 'pwd/udai_paypwd_modify.html')


# 修改登录密码
def udai_pwd_modify(request):
    return render(request, 'pwd/udai_pwd_modify.html')


# 我的订单
def udai_order(request):
    return render(request, 'order/udai_order.html')


# 我的收藏
def udai_collection(request):
    return render(request, 'user/udai_collection.html')


# 退款退货
def udai_refund(request):
    return render(request, 'user/udai_refund.html')


# 物流查询
def udai_mail_query(request):
    return render(request, 'user/udai_mail_query.html')


# 售后服务
def udai_article1(request):
    return render(request, 'temp_article/udai_article1.html')


# 配送服务
def udai_article2(request):
    return render(request, 'temp_article/udai_article2.html')


# 用户协议
def udai_article3(request):
    return render(request, 'temp_article/udai_article3.html')


# 常见问题
def udai_article4(request):
    return render(request, 'temp_article/udai_article4.html')


# 如何成为代理商
def udai_article5(request):
    return render(request, 'temp_article/udai_article5.html')


# 代销商上架教程
def udai_article6(request):
    return render(request, 'temp_article/udai_article6.html')


# 分销商常见问题
def udai_article7(request):
    return render(request, 'temp_article/udai_article7.html')


# 付款账户
def udai_article8(request):
    return render(request, 'temp_article/udai_article8.html')


# 企业简介
def udai_article10(request):
    return render(request, 'temp_article/udai_article10.html')


# 加入商城
def udai_article11(request):
    return render(request, 'temp_article/udai_article11.html')


# 隐私说明
def udai_article12(request):
    return render(request, 'temp_article/udai_article12.html')


# 留言
def issues(request):
    return render(request, 'user/issues.html')


# 课堂
def class_room(request):
    return render(request, 'user/class_room.html')


# 企业账号
def enterprise_id(request):
    return render(request, 'user/enterprise_id.html')


# 诚信合约
def udai_contract(request):
    return render(request, 'user/udai_contract.html')


# 平台公告
def udai_notice(request):
    return render(request, 'user/udai_notice.html')


# 练习我们
def udai_about(request):
    return render(request, 'user/udai_about.html')


# 修改支付密码1
def udai_modifypay_step1(request):
    return render(request, 'pwd/udai_modifypay_step1.html')


# 修改支付密码2
def udai_modifypay_step2(request):
    return render(request, 'pwd/udai_modifypay_step2.html')


# 修改支付密码3
def udai_modifypay_step3(request):
    return render(request, 'pwd/udai_modifypay_step3.html')


# 修改登录密码1
def udai_modifypwd_step1(request):
    return render(request, 'pwd/udai_modifypwd_step1.html')


# 修改登录密码2
def udai_modifypwd_step2(request):
    return render(request, 'pwd/udai_modifypwd_step2.html')


# 修改登录密码3
def udai_modifypwd_step3(request):
    return render(request, 'pwd/udai_modifypwd_step3.html')


def address_edit(request):
    return render(request, 'user/udai_address_edit.html')


def udai_article(request):
    return render(request, 'user/udai_article.html')


def udai_message(request):
    return render(request, 'user/udai_message.html')
